#include "tcpclient.h"


TcpClient::TcpClient(QObject* object) {
    m_tcpSocket = new QTcpSocket(object);

    m_tcpSocket->connectToHost("192.168.0.100", 9988);

    //成功连接
    connect(m_tcpSocket, &QTcpSocket::connected, this, [=]() {
        m_tcpConnectStatus = m_tcpSocket->state();
    });

    //断开连接
    connect(m_tcpSocket, &QTcpSocket::disconnected, this, [=]() {
        m_tcpConnectStatus = m_tcpSocket->state();
    });

    //接受到服务器信息
    connect(m_tcpSocket, &QTcpSocket::readyRead, this, [=](){
        while (m_tcpSocket->bytesAvailable() > 0)
        {
            m_receiveData = m_tcpSocket->readAll();
            qDebug()<<QString(m_receiveData);
        }
        emit(receiveData(QString(m_receiveData)));
    });

}

TcpClient::~TcpClient()
{
    // 结束连接
    m_tcpSocket->disconnectFromHost();
    delete m_tcpSocket;
}

void TcpClient::sendMessage(QByteArray data)
{
    m_tcpSocket->write(data);
}

int TcpClient::getConnectStatus()
{
    return m_tcpSocket->state();
}
